#include "SQLDropUserStatement.h"
#include "SQLDDLStatement.h"
#include "../../visitor/SQLASTVisitor.h"
#include "../../../utils/listTran.h"

SQLDropUserStatement::SQLDropUserStatement()
{
  users = std::make_shared<std::list<SQLExpr_ptr>>();
}

SQLDropUserStatement::SQLDropUserStatement(DbType_ptr  dbType)
    : SQLStatementImpl(dbType)
{
  users = std::make_shared<std::list<SQLExpr_ptr>>();
}

BOOL_ptr SQLDropUserStatement::isIfExists()
{
  return ifExists;
}

void SQLDropUserStatement::setIfExists(BOOL_ptr ifExists)
{
  this->ifExists = ifExists;
}

SQLExpr_list_ptr  SQLDropUserStatement::getUsers()
{
  return users;
}

void SQLDropUserStatement::addUser(SQLExpr_ptr user)
{
  if (user != NULL)
  {
    user->setParent(SharedObject(SQLDropUserStatement));
  }
  this->users->push_back(user);
}

void SQLDropUserStatement::accept0(SQLASTVisitor_ptr visitor)
{
  if (visitor->visit(SharedObject(SQLDropUserStatement)))
  {
    SQLObject_list_ptr  tmp = ListTran::tran_class<SQLExpr, SQLObject>(users);
    acceptChild(visitor, tmp);
  }
  visitor->endVisit(SharedObject(SQLDropUserStatement));
}

SQLExpr_list_ptr  SQLDropUserStatement::getChildren()
{
  return users;
}

BOOL_ptr SQLDropUserStatement::replace(SQLExpr_ptr expr, SQLExpr_ptr target)
{
  auto it_users = users->begin();
  for (int i = 0; i < users->size(); i++)
  {
    std::advance(it_users, i);
    if (*it_users == expr)
    {
      target->setParent(SharedObject(SQLDropUserStatement));
      *it_users = target;
      return BOOL::TRUE;
    }
  }

  return BOOL::FALSE;
}

SQLDDLStatement::DDLObjectType SQLDropUserStatement::getDDLObjectType()
{
  return SQLDDLStatement::DDLObjectType::USER;
}
